#include "../Include/ConnectionDefinition.hpp"

using Supetronix::PersistO::DL::ConnectionDefinition;

std::map<ConnectionSourceName, ConnectionDefinition*>* ConnectionDefinition::_definitionStore = new std::map<ConnectionSourceName, ConnectionDefinition*>();

void ConnectionDefinition::Add(ConnectionSourceName sourceName, ConnectionSourceType sourceType, string serverName, string userID, string password, string databaseName, int minPoolSize, int maxPoolSize)
{
	//minimum pool size is zero and at least we need 10 connections pool. Later on we can make the connection pooling optional
    if (minPoolSize < 0)
            minPoolSize = 0;

    if (maxPoolSize < 10)
            maxPoolSize = 10;
    
    ConnectionDefinition* c = new ConnectionDefinition();
    c->_sourceName = sourceName;
    c->_sourceType = sourceType;
    c->_serverName = serverName;
    c->_userID = userID;
    c->_password = password;
    c->_databaseName = databaseName;
    c->_minPoolSize = minPoolSize;
    c->_maxPoolSize = maxPoolSize;
    
	//add the connection definition to internal cache
    _definitionStore->insert(std::make_pair(sourceName, c));
    
    c = nullptr;
}

const ConnectionDefinition* ConnectionDefinition::Get(ConnectionSourceName sourceName)
{
	//return the connection definition for the specific source
    return _definitionStore->at(sourceName);
}